package com.itheima.tanhua.web;


import com.itheima.tanhua.Dto.MovementDto;
import com.itheima.tanhua.service.CommentService;
import com.itheima.tanhua.service.MovementService;
import com.itheima.tanhua.vo.MovementsVo;
import com.itheima.tanhua.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/movements")
public class MovementController {

    @Autowired
    private MovementService movement1Service;
    @Autowired
    RedisTemplate<String,String> redisTemplate;


    @PostMapping
    public ResponseEntity saveMovements(MovementDto movementDto, MultipartFile[] imageContent) {

        movement1Service.saveMovements(movementDto, imageContent);

        return ResponseEntity.ok(null);
    }

    //我的动态
    @GetMapping("/all")
    public ResponseEntity findMovementAll(Long userId,
                                          @RequestParam(value = "page", defaultValue = "1") Integer page,
                                          @RequestParam(value = "pagesize", defaultValue = "10") Integer pagesize) {

        PageResult pageResult = movement1Service.findMovementAll(userId, page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    @GetMapping
    public ResponseEntity findFriendMovement(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                             @RequestParam(value = "pagesize", defaultValue = "10") Integer pagesize) {

        PageResult pageResult = movement1Service.findFriendMovement(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 推荐圈子
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/recommend")
    public ResponseEntity findMovements(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                        @RequestParam(value = "pagesize", defaultValue = "10") Integer pagesize) {
        PageResult pageResult = movement1Service.findMovements(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }



    @GetMapping("/{id}")
    public ResponseEntity findOneById(@PathVariable("id") String movementId) {
        MovementsVo oneById = movement1Service.findOneById(movementId);
        return ResponseEntity.ok(oneById);
    }

    //TODO 谁看过我
    @GetMapping("/visitors")
    public ResponseEntity visitors() {
        return ResponseEntity.ok(new Object[]{});
    }

    /**
     * 动态喜欢+1
     *
     * @param movementId 动态id
     * @return
     */
    @GetMapping("/{id}/love")
    public ResponseEntity loveById(@PathVariable("id") String movementId) {
        ValueOperations<String, String> stringStringValueOperations = redisTemplate.opsForValue();
        String uid = stringStringValueOperations.get("CHECK_ID");
        //动态喜欢数加一
        Integer count = movement1Service.updateLoveCount(movementId);
        redisTemplate.opsForValue().set(uid+movementId+"love",count+"");
        return ResponseEntity.ok(count);
    }

    /**
     * 动态点赞+1
     *
     * @param movementId 动态id
     * @return
     */
    @GetMapping("/{id}/like")
    public ResponseEntity likeById(@PathVariable("id") String movementId) {
        ValueOperations<String, String> stringStringValueOperations = redisTemplate.opsForValue();
        String uid = stringStringValueOperations.get("CHECK_ID");
        //动态点赞数加一
        Integer count = movement1Service.updateLikeCount(movementId);
        redisTemplate.opsForValue().set(uid+movementId,count+"");
        return ResponseEntity.ok(count);
    }
    /**
     * 取消点赞-1
     *
     * @param movementId 动态id
     * @return
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity dislikeById(@PathVariable("id") String movementId) {
        ValueOperations<String, String> stringStringValueOperations = redisTemplate.opsForValue();
        String uid = stringStringValueOperations.get("CHECK_ID");
        //取消点赞
        Integer count = movement1Service.updateDisLike(movementId);
        redisTemplate.delete(uid+movementId);
        return ResponseEntity.ok(count);
    }

    /**
     * 取消喜欢-1
     *
     * @param movementId 动态id
     * @return
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity disloveById(@PathVariable("id") String movementId) {
        ValueOperations<String, String> stringStringValueOperations = redisTemplate.opsForValue();
        String uid = stringStringValueOperations.get("CHECK_ID");
        //取消点赞
        Integer count = movement1Service.updateDisLove(movementId);
        redisTemplate.delete(uid+movementId+"love");
        return ResponseEntity.ok(count);
    }
}
